package org.jeecg.modules.xxgx.common.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ObjectUtils {

    public static boolean isEmpty(Object obj){
        return isEmpty(obj, (String) null);
    }

    public static boolean isEmpty(Object obj,String... excludeFields){
        Map<String, Object> map = BeanUtil.beanToMap(obj);
        if(map.isEmpty()){
            return true;
        }
        for (Map.Entry<String, Object> entry: map.entrySet()) {
            Object value = entry.getValue();
            if(!ArrayUtil.contains(excludeFields,entry.getKey()) && value != null && StringUtils.isNotBlank(value.toString())){
                return false;
            }
        }
        return true;
    }


    public static <T> List<T> filterEmpty(List<T> objs,String... excludeFields){
        return objs.stream().filter(obj-> !isEmpty(obj,excludeFields)).collect(Collectors.toList());
    }

    public static <T> List<T> filterEmpty(List<T> objs){
        return objs.stream().filter(obj-> !isEmpty(obj)).collect(Collectors.toList());
    }
}
